﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmitMapper;
using WebSite.Core;
using WebSite.Data.NH.Repositories;
using WebSite.Model;
using WebSite.Web.Models;
using WebSite.Web.SessionAndFilter;

namespace WebSite.Web.Controllers.Admin
{
    [AuthorAttribute]
    public class ContentController : Controller
    {
        private readonly IContentRepository content;
        //
        // GET: /Content/

        public ContentController(IContentRepository content)
        {
            this.content = content;
        }
        public ActionResult Index(int categoryId = 0)
        {
            ViewBag.categoryId = categoryId;
            return View();
        }
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="np"></param>
        /// <returns></returns>
        public JsonResult GetData(string title,int categoryId = 0, int page = 1, int pageSize = 15)
        {
            var user = Sessions.User;

            GridData<ContentVM> data = new GridData<ContentVM>();

            ObjectsMapper<ContentInfo, ContentVM> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<ContentInfo, ContentVM>();

            var where = PredicateExtension.True<ContentInfo>();
            where = where.AndAlso(u => u.CompanyId == user.CompanyId);
            where = where.AndAlso(u => u.CategoryId == categoryId);

            if (!string.IsNullOrEmpty(title))
            {
                where = where.AndAlso(u => u.Title.Contains(title.Trim()));
            }

            var count = content.LoadCount(where);
            var list = content.LoadAll(where, page, pageSize, new OrderByExpression<ContentInfo, DateTime>(u => u.ReleaseDate, false));
            data.Rows = mapper.MapEnum(list).ToList();
            data.Total = count;
            return Json(data);
        }

        public ActionResult Edit(int id = 0,int categoryId = 0)
        {
            ContentVM vm = null;
            if (id == 0)
            {
                vm = new ContentVM();
                vm.CompanyId = Sessions.User.CompanyId;
                vm.CategoryId = categoryId;
                vm.AdminUserId = Sessions.User.UserId;
                vm.AdminUserName = Sessions.User.TrueName;
            }
            else
            {
                ObjectsMapper<ContentInfo, ContentVM> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<ContentInfo, ContentVM>();
                ContentInfo ci = content.GetContent(id);
                vm = mapper.Map(ci);
                vm.Content = ci.ContentDetail.Content;
            }
            ViewBag.categoryId = categoryId;
            return View(vm);
        }


        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(ContentVM vm)
        {
            if (ModelState.IsValid)
            {
                ObjectsMapper<ContentVM, ContentInfo> mapper =
                  ObjectMapperManager.DefaultInstance.GetMapper<ContentVM, ContentInfo>();

                var user = Sessions.User;

                if (vm.ContentId == 0)
                {
                    var a = mapper.Map(vm);
                    a.ReleaseDate = DateTime.Now;
                    a.ContentDetail = new ContentDetailInfo { Content = vm.Content };
                    a.ContentDetail.ContentInfo = a;
                    bool res = content.AddContent(a);
                    return Json(res);
                }
                else
                {
                    var a1 = content.GetContent(vm.ContentId);
                    a1.Title = vm.Title;
                    a1.ImgUrl = vm.ImgUrl;
                    a1.ContentDetail.Content = vm.Content;
                    bool res = content.UpdateContent(a1);
                    return Json(res);
                }

            }

            return View();
        }


        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                bool result = false;
                result = content.DeleteContent(id);
                return Json(result);
            }
            catch
            {
                return Json(false);
            }
        }
    }
}
